package com.muslimedia.reloadmanager.util;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;

public class SMSReceiver extends BroadcastReceiver {
	private final String KEY_HEADER_ISI = "ISI";
	private final String KEY_HEADER_CEKHUTANG = "CH";
	private final String WRONG_MESSAGE = "FALSE";
	private final String PIN = "12345";
	private final String DELIMITER = "#";

	@Override
	public void onReceive(Context context, Intent intent) {
		//---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
            	msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            	String msgBody = msgs[i].getMessageBody().toString();
            	String sender = msgs[i].getOriginatingAddress();
            	String receiver = "5556";
            	
            	StringBuilder m = new StringBuilder();
            	StringBuilder s = new StringBuilder();
            	StringBuilder r = new StringBuilder();
            	
            	setValue(m, msgBody);
            	setValue(s, sender);
            	setValue(r, receiver);
            	
            	boolean flag = smsProcess(m, s, r);
            	if(flag){
            		SmsManager sm = SmsManager.getDefault(); 
        			sm.sendTextMessage(r.toString(), null, m.toString(), null, null);
            	}
            }
        }
	}
	
	private boolean smsProcess(StringBuilder msgBody, StringBuilder sender, StringBuilder receiver){
    	String id = null;
    	//cek sender apakah verified?
    	
    	if(msgBody.toString().contains(DELIMITER)){
    		String []  vars = msgBody.toString().split(DELIMITER);
    		int n = vars.length;
    		id = vars[0].toUpperCase();
    		
    		//-- request to reload
    		if(id.equals(KEY_HEADER_ISI)){
    			String nominal = null;
    			switch(n){
    			case 2 :
    				nominal = vars[1].trim();
    				if(isNumeric(nominal)){
    					String mb = String.format("%s.%s.%s", nominal, sender, PIN);
    					setValue(msgBody, mb);
    					return true;
    				}else{
    					return false;
    				}
    			case 3: 
    				nominal = vars[1].trim();
    				if(isNumeric(nominal)){
    					String x = vars[2].trim();
    					if(x.contains("x")){
    						return false;
    					}else{
    						if(isNumeric(x.substring(1)) && x.charAt(0)=='0'){
    							String mb = String.format("%s.%s.%s", nominal, x, PIN);
    							setValue(msgBody, mb);
        						return true;
    						}else if (x.length() > 15){
    							return false;
    						}else{
    							receiver = sender;
    							String mb = "Format of phone number ("+x+") invalid.";
    							setValue(msgBody, mb);
    							return true;
    						}
    					}
    					
    				}else{
    					return false;
    				}
    			}
    		}
    	}
    	return false;
	}
	
	private boolean isNumeric(String s){
		try{
			Integer.parseInt(s);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	private void setValue(StringBuilder sb, String s){
    	sb.delete(0, sb.length()).append(s);
	}
}
